package com.booktrade.controller;

import com.booktrade.entity.Messages;
import com.booktrade.result.Result;
import com.booktrade.service.IMessagesService;
import com.booktrade.util.BaseContext;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.message.Message;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 私信表，存储用户之间的消息 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/messages")
@RequiredArgsConstructor
public class MessagesController {
    private final IMessagesService messageService;

    @PostMapping("/send")
    @Operation(summary = "发送私信")
    public Result<?> sendMessage(@RequestParam Integer receiverId, @RequestParam String content) {
        Integer senderId = BaseContext.getCurrentId();
        messageService.sendMessage(senderId, receiverId, content);
        return Result.success();
    }

    @GetMapping("/list")
    @Operation(summary = "获取与某个联系人的私信记录")
    public Result<?> getMessages(@RequestParam Integer contactId) {
        Integer userId = BaseContext.getCurrentId();
        List<Messages> messages = messageService.getMessagesWithContact(userId, contactId);
        return Result.success(messages);
    }
    @GetMapping("/contacts")
    @Operation(summary = "获取用户的所有私聊联系人")
    public Result<List<Integer>> getContacts() {
        Integer userId = BaseContext.getCurrentId();
        List<Integer> contacts = messageService.getAllContacts(userId);
        return Result.success(contacts);
    }
}
